/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.thread;

import com.ttt.nor2.alarm.MsgExchage;
import com.ttt.nor2.config.CfgAlarm;
//import java.sql.Connection;
import com.ttt.nor2.util.ExceptionUtil;
import org.apache.log4j.Logger;

/**
 *
 * @author anuphabm
 */
public class ThSweepFillter extends Thread {

    public static int thWork = 0;
    private Logger logger = Logger.getLogger(ThSweepFillter.class);
    private CfgAlarm cfg;
    private Class c = null;
    //private Connection conn;

    public static int getThWork() {
        return thWork;
    }
    static synchronized void incThread(){
        thWork++;
    }

    synchronized void decThread(){
        thWork--;
    }
    public  ThSweepFillter(CfgAlarm cfg) {
        this.cfg = cfg;
        incThread();
        logger.debug("Thread working...[" + thWork + "]");
    }

    @Override
    public void run() {
        MsgExchage msgExchange = null;
        try {
            //super.run();
            // read file
            c = Class.forName("com.ttt.nor2.alarm." + cfg.getClassname());
            logger.info("Run class name [" + cfg.getClassname() + "]");
            msgExchange = (MsgExchage) c.newInstance();
            msgExchange.setCfg(cfg);
            msgExchange.readAlarmFromFile2String();
            if (msgExchange.isSavedata()) {
                msgExchange.deleteFile();
            }
        } catch (Exception ex) {
            logger.error("Exception can not run class " + ExceptionUtil.getStackTrace(ex));
            msgExchange.deleteFile();
        } finally {
            logger.debug("Thread end working...[" + thWork + "]");
            decThread();
        }
    }
    /*
    @Override
    protected void finalize() throws Throwable {
        //log.debug("Set row = 1");
        //iptf.setRow(1);// cleare row == 1
        logger.debug("Thread end working...[" + thWork + "]");
        --thWork;
    }
    */
}
